# -*- coding: utf-8 -*-
"""
name:额度-重庆
code：HQD_pricing_CQ

@author: maggieliu
"""

import math
import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')
from decimal import Decimal

#指标缺失值转化为0
def null_trans(index_name):
    if index_name == u"缺失值":
        return 0.0
    else:
        try:
            value = float(index_name)
        except:
            value = 0.0
        return value

#宏观环境模块调整系数
en_score=null_trans({HQDEN_score})

def en_rating(x):
    if x >=63.95710234:
        return "A"
    if x<63.95710234 and x >= 51.18228:
        return "B"
    if x<51.18228 and x>=44.90874:
        return "C"
    if x<44.90874 and x >=39.15593343:
        return "D"
    if x<39.15593343:
        return "E"
    else:
        return u"缺失值"

#缺失值判定为1是为了规避宏观数据异常的情况，减小对模型评级流程的影响。但如发现缺失值可考虑进行预警。
en_rating1=en_rating(en_score)
en_coe_dict = {'A': 1.04, 'B': 1, 'C': 1,'D':1 ,'E': 0.97,u"缺失值":1}
en_ad = en_coe_dict[en_rating1]


#还款意愿调整系数


rating_level = decisionModelResult.get("ratingLevel")
pay_coe_dict = {'A': 1.5, 'B': 1.19, 'C': 1.05,'D':0.86 ,'E':0.4,'F':0,u'缺失值':0}
pay_ad = pay_coe_dict[rating_level]


#经营情况调整系数
#实际利润及一级行业交互
profit_indust={TX07_ActualProfitWithIndustry}


def profit_indus_score(x):
    if x in ['4',4,'5',5]:
        return 55
    if x in ['3',3]:
        return 35
    if x in ['1',1,'2',2]:
        return 30
    if x==u"缺失值":
        return  35
Revenue_Indus_score=profit_indus_score(profit_indust)

#年均申报收入
Avg_Year_Income ={TX02_MonthlyIncomeReportAmt}

def AvgIncome_score(x):
    if x == u"缺失值":
        return 40
    else:
        x1=float(x)*12
        if x1 >=30000000:
            return 70
        if x1>=10000000 and x1<30000000:
            return 50
        if x1>=3000000 and x1<10000000:
            return 40
        if x1>= -999999999 and x1<3000000:
            return 20
Avg_Year_Income_score = AvgIncome_score(Avg_Year_Income)



#纳税评级
tax_rating = {TX08_TaxRating}

def tax_score(x):
    if x==u'缺失值':
        return 60
    if x =="A":
        return 100
    if x=="B":
        return 60
    if x=="C":
        return 30
    if x=="D":
        return 0
    if x=="M":
        return 0
tax_rating_score = tax_score(tax_rating)
#负债收入比


#计算负债
liability=float({pricing_amount_ECindex})*10000 + float({pricing_amount_ICindex})

#收入
def income1():
    income=Avg_Year_Income
    if income == u"缺失值":
        return 0
    else:
        return float(income)*12
income=income1()

#负债收入比
def lia_income_ratio():
    if income<0 or liability<0:
        return u"缺失值"
    if income ==0:
        return 0
    else:
        return liability/income


lia_income_ratio = lia_income_ratio()


#负债收入比得分
def lia_income_score(x):
    if x == u"缺失值":
        return 35
    else:
        if x == 0:
            return 35
        if x >0 and x<=0.1:
            return 60
        if x>0.1 and x<=0.3:
            return 40
        if x>0.3 and x<=0.6:
            return 20
        if x>0.6:
            return 0

lia_income_score=lia_income_score(lia_income_ratio)



#经营状况模块
oper_score=Revenue_Indus_score*0.25+Avg_Year_Income_score*0.25+tax_rating_score*0.25+lia_income_score*0.25
#经营情况模块

def oper_rating(x):
    if x<=100 and x>=55.625:
        return "C"
    if x<55.625 and x >=38.125:
        return "B"
    if x<38.125:
        return "A"
    else:
        return u"缺失值"

oper_rating=oper_rating(oper_score)

oper_coe_dict = {'A': 0.85, 'B': 1.01, 'C': 1.05,u'缺失值':0}
oper_ad=oper_coe_dict[oper_rating]


#计算TAX1
def TAX(x):
    tax=x
    if tax in [u"缺失值" ,-0.001]:
        return 0
    else:
        return float(tax)


tax1=TAX({R41_TX_EntAnnualValueAddeTaxAmtCheck})
tax2=TAX({R41_TX_EntAnnualIncomeTaxAmtCheck})

#新增行业校验


def hydm():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    hydm_dict = ['5132','5173','5172','5264','5263','5261','5431','5523',5132,5173,5172,5264,5263,5261,5431,5523]
    try:
        hydm = {HYDM_CQ}
        if hydm in hydm_dict:
            return 1
        else:
            return 0
    except:
        return 0

hydm=hydm()


#取经营年限
#opyear=TAX({R05_EG_EntRegisterCheck})

def ad_amount():
    amount_dict = {"A":{1:1000000,0:1000000},\
                 "B":{1:1000000,0:1000000},\
                 "C":{1:700000,0:1000000},\
                 "D":{1:500000,0:1000000},\
                 "E":{1:300000,0:1000000},\
                 "F":{1:0,0:0}}
    ad_amount=amount_dict[rating_level][hydm]
    opyear=TAX({R05_EG_EntRegisterCheck})
    if opyear >=0 and opyear<2:
        return 0
    if opyear >=2 and opyear <3:
        return min(ad_amount,500000)
    if opyear>=3:
        return ad_amount

ad_amount =ad_amount()

#基础额度

basic_amount=min((tax1+tax2)*4.385545,1000000)
#调整额度
adjust_amount =basic_amount*oper_ad*pay_ad*en_ad

#授信额度
#资金需求额度

#纳税准入限制

request_amount = float(ratingRequest.get('amount'))
credit_amount= min(request_amount,adjust_amount,ad_amount)
credit_amount1=round(credit_amount/100000,1)*100000


result = credit_amount1